<!--#include file="lib/utf-8.asp"-->
<!--#include file="lib/conn.asp"-->
<!--#include file="lib/saferequest.asp"-->
<!--#include file="lib/error_write.asp"-->
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="Expires" content="-1" />
<link rel="stylesheet" href="lib/jQuery.ValidationEngine/css/validationEngine.jquery.css" type="text/css">
<link rel="stylesheet" href="lib/sheet.css" type="text/css">
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true&language=zh-CN"></script>
<script type="text/javascript" src="lib/jquery_lastest.js"></script>
<script type="text/javascript" src="lib/jQuery.ValidationEngine/js/jquery.validationEngine-cn.js"></script>
<script type="text/javascript" src="lib/jQuery.ValidationEngine/js/jquery.validationEngine.js"></script>
<script type="text/javascript">
	$(function() {
		var press_c_addr_open=false;
		$(".contable").addClass("ct");	//载入table样式
		$(".contable th").addClass("ch").css("background-color","silver");	//载入th样式
		$(".contable td").addClass("ld");	//载入td样式
		$("#form1").validationEngine();
		$("#tj").click(function(){
			//if($("#customer_name").val()==""){
			//	alert("请输入客户名称！");
			//	$("#customer_name").focus();
			//	return false;
			//}
			
			
			
			
			//reg=new RegExp("[\"\\\\\\/\t\v\f\n{}',<>%]");
			//reg=new RegExp("[\042\/\t',<>\{\}\[\]%]","m");
			//if(reg.test($("#customer_memo").val())){
			//	alert("注意事项中存在非法字符");
			//	return false;
			
			//}
			if($("#lat").val()!=""&&$("#lng").val()!=""){
				if($("#customer_address").val()!=$("#customer_address_old").val()){
					if(!press_c_addr_open){
						alert("客户地址已经改变，请重新定位！");
						return false;
					}else{
						return true;
					}
				}else{			
					return true;
				}
			}else{
				alert("请先定位客户地址！");
				return false;
			}
		
		});
		$("#customer_name").bind("keyup focusout",function(){
			if($(this).val()!=""){
				$.get("h2p.asp?hz="+$(this).val(),function(py){
					$("#customer_pinyin_l").val(py);
				});
			}else{
				$("#customer_pinyin_l").val("");
			}
		});
		
		
		$("#c_addr_open").click(function(){
			if(!$("#customer_address").val()==""){
				marker.setMap(null);
				codeAddress();
				press_c_addr_open=true;
			}else{
				alert("请输入客户地址！");
				$("#customer_address").focus();
			}
		
		
		});
		var map;
		var myLatlng;
		var	geocoder;
		var myLat=29.868336;
		var myLng=121.54399;
		var myZoom=6;
		var marker;
		var infowindow;
		geocoder = new google.maps.Geocoder();
		marker=new google.maps.Marker();
		infowindow = new google.maps.InfoWindow();
		//地图初始化
		if($("#lat").val()!=""){myLat=$("#lat").val();}
		if($("#lng").val()!=""){myLng=$("#lng").val();}
		if($("#lat").val()!=""&&$("#lng").val()!=""){myZoom=16;}
		myLatlng = new google.maps.LatLng(myLat,myLng );
		var mapOptions = {
			zoom: myZoom,                //缩放级别
			center: myLatlng,       //将地图的中心设置为指定的地理点 可以使用 0（最低缩放级别，在地图上可以看到整个世界）到 19（最高缩放级别，可以看到独立建筑物）之间的缩放级别
			mapTypeId: google.maps.MapTypeId.ROADMAP,   //ROADMAP-默认视图 SATELLITE-显示Google地球卫星图像 HYBRID-混合显示普通视图和卫星视图 TERRAIN-地形图
			mapTypeControl: true,
			mapTypeControlOptions: {
				style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
			},
			navigationControl: true,
			navigationControlOptions: {
				style: google.maps.NavigationControlStyle.SMALL,
				position: google.maps.ControlPosition.LEFT
			},
			scaleControl: true,    //比例尺
			scaleControlOptions: {
				position: google.maps.ControlPosition.BOTTOM_LEFT
			}
		};
		map = new google.maps.Map(document.getElementById("map_canvas"),mapOptions);//编写 JavaScript 函数创建“map”对象
		marker.setOptions({
				map: map,
				draggable: true,
				position: myLatlng,
				title:$("#customer_name").val()
		});
		var contentString = '<div id="content">'+
			'<h1 id="firstHeading">'+$("#customer_name").val()+'</h1>'+
			'<div id="bodyContent">'+
			'<p>地址：' +$("#customer_address").val()+'</p>'+
			'<p>联系人：' +$("#customer_owner").val()+'</p>'+
			'<p>联系电话：' +$("#customer_phone").val()+'</p>'+
			'<p>注意事项：' +$("#customer_memo").val()+'</p>'+
			'</div>'+
			'</div>';

		infowindow.setOptions({
				content: contentString
		});

		infowindow.open(map,marker);
		google.maps.event.addListener(marker, 'click', function() {
			infowindow.open(map,marker);
		});
		google.maps.event.addListener(marker,'dragend',function(){
			myLatlng=marker.getPosition();
			$("#lat").val(myLatlng.lat());
			$("#lng").val(myLatlng.lng());
		});
		
		function codeAddress() {
			var address = $("#customer_address").val();
			if (geocoder) {
			 geocoder.geocode( { 'address': address}, function(results, status) {
					if (status == google.maps.GeocoderStatus.OK) {	
					$("#lat").val(results[0].geometry.location.lat());
					$("#lng").val(results[0].geometry.location.lng());
					map.setZoom(16);
					
						map.setCenter(results[0].geometry.location);
						marker.setOptions({
								map: map,
								draggable: true,
								position: results[0].geometry.location,
								title:$("#customer_name").val()
						});
						var contentString = '<div id="content">'+
							'<h1 id="firstHeading">'+$("#customer_name").val()+'</h1>'+
							'<div id="bodyContent">'+
							'<p>地址：' +$("#customer_address").val()+'</p>'+
							'<p>联系人：' +$("#customer_owner").val()+'</p>'+
							'<p>联系电话：' +$("#customer_phone").val()+'</p>'+
							'<p>注意事项：' +$("#customer_memo").val()+'</p>'+
							'</div>'+
							'</div>';

						infowindow.setOptions({
								content: contentString
						});

						google.maps.event.addListener(marker, 'click', function() {
							infowindow.open(map,marker);
						});
						google.maps.event.addListener(marker,'dragend',function(){
							myLatlng=marker.getPosition();
							$("#lat").val(myLatlng.lat());
							$("#lng").val(myLatlng.lng());
						});
					} else {
						alert("地图上未找到当前地址: " + status);
						$("#lat").val("");
						$("#lng").val("");
					}
				});
			}
		}
	
	});
</script>	
</head>
<%
call CheckRights(1,4)
%>

<body>
<%
if request.QueryString("customer_id")<>"" then customer_id=SafeRequest("customer_id",1) Else response.end
set rs = Server.CreateObject("ADODB.Recordset")
sql="select * from (SELECT * FROM customer where customer_id="&customer_id&") as c left join yh on c.customer_user=yh.id"
rs.open sql,conn,1,1
If rs.eof Then
	rs.close
	set rs=nothing
	error_write 1,"未找到记录！"
End if
if rs("customer_user")<>session("MM_ID") and not CheckPF(1,15) then
	error_write 2,"无此客户管理权限！"
end if

if request.form("tj")="编辑" then
	customer_name=SafeRequest("customer_name",0)
	customer_pinyin_l=SafeRequest("customer_pinyin_l",0)
	customer_address=SafeRequest("customer_address",0)
	equip_address=SafeRequest("equip_address",0)
	customer_owner=SafeRequest("customer_owner",0)
	customer_phone=SafeRequest("customer_phone",0)
	customer_memo=SafeRequest("customer_memo",0)
	customer_salesman=SafeRequest("customer_salesman",1)
	customer_address_lat=SafeRequest("lat",1)
	customer_address_lng=SafeRequest("lng",1)
	if request("customer_user")<>"" then customer_user=SafeRequest("customer_user",1) else customer_user=0
	sql="select customer_name,customer_address from customer where customer_id<>"&customer_id&" and (customer_address='"&customer_address&"')"
	set rs_check = Server.CreateObject("ADODB.Recordset")
	rs_check.open sql,conn,1,1
	if not rs_check.eof then
		error_content="已经存在的记录：<br />"
		do while not rs_check.eof
			error_content=error_content&rs_check("customer_name")&" "&rs_check("customer_address")&"<br />"
			rs_check.movenext
		loop
		rs_check.close
		set rs_check=nothing
		error_write 2,"客户名称或客户地址重复！<br />"&error_content
	end if
	
	
	sql="update customer set customer_name='"&customer_name&"',customer_address='"&customer_address&"',equip_address='"&equip_address&"',customer_owner='"&customer_owner&"',customer_phone='"&customer_phone&"',customer_memo='"&customer_memo&"',customer_salesman="&customer_salesman&",customer_address_lat="&customer_address_lat&",customer_address_lng="&customer_address_lng&",customer_pinyin_l='"&customer_pinyin_l&"',customer_user="&customer_user&" where customer_id="&customer_id 
	'response.write sql
	'response.end
	set cmd=Server.CreateObject("ADODB.Command")
	cmd.ActiveConnection=conn
	cmd.CommandText = sql
	cmd.Execute
	view_url="customer.asp"
	response.Redirect(view_url)
end if

%>
<div><a href="customer.asp">客户信息管理</a>&gt;&gt;<a href="edit_customer.asp?customer_id=<%=customer_id%>">编辑客户</a>&gt;&gt;</div>
<form id="form1" name="form1" method="post" action="#">
<table class="contable">
	<tr>
		<th colspan="2" style="font-size:16px">编辑客户</th>
	</tr>
	<tr>
		<th>客户名称</th>
		<td>
			<input class="validate[required,custom[customer_name]]" id="customer_name" name="customer_name" maxlength="50" size="50" value="<%=rs("customer_name")%>" />
			拼音缩写<input id="customer_pinyin_l" name="customer_pinyin_l" readonly="readonly" value="<%=rs("customer_pinyin_l")%>" />
		</td>
	</tr>
	<tr>
		<th>客户地址</th>
		<td>
			<input class="validate[required,custom[customer_address]]" id="customer_address" name="customer_address" maxlength="250" size="80" value="<%=rs("customer_address")%>" />
			<input id="customer_address_old" name="customer_address_old" type="hidden" value="<%=rs("customer_address")%>" />
			<button type="button" id="c_addr_open" name="c_addr_open">定位客户地址</button>
			<input id="lat" name="lat" type="hidden" value="<%=rs("customer_address_lat")%>" />
			<input id="lng" name="lng" type="hidden" value="<%=rs("customer_address_lng")%>" />
		</td>
	</tr>
	<tr>
		<th>装箱地点</th>
		<td><select id="equip_address" name="equip_address">
			<%
				sql="select DISTINCT didian from yj "
				set rs_equip_address=server.createobject("adodb.recordset")
				rs_equip_address.open sql,conn,1,1

				while not rs_equip_address.eof
			%>
				<option value="<%=rs_equip_address("didian")%>" <%if rs_equip_address("didian")=rs("equip_address") then response.write("selected") %>><%=rs_equip_address("didian")%></option>
			<%
					rs_equip_address.movenext()
				Wend
				rs_equip_address.close
				set rs_equip_address=nothing
			%>
			</select></td>
	</tr>
	<tr>
		<th>联系人</th>
		<td><input class="validate[required,custom[customer_owner]]" id="customer_owner" name="customer_owner" maxlength="50" size="50" value="<%=rs("customer_owner")%>" /></td>
	</tr>
	<tr>
		<th>联系电话</th>
		<td><input class="validate[optional,custom[customer_phone]]" id="customer_phone" name="customer_phone" maxlength="50" size="50" value="<%=rs("customer_phone")%>" /></td>
	</tr>
	<tr>
		<th>注意事项</th>
		<td><input class="validate[optional,custom[customer_memo]]" maxlength="250" size="80" id="customer_memo" name="customer_memo" value="<%=rs("customer_memo")%>" /></td>
	</tr>
	<tr>
		<th>业务员</th>
		<td>
			<select id="customer_salesman" name="customer_salesman">
			
			<%
				sql="select * from salesman "
				set rs_salesman=server.createobject("adodb.recordset")
				rs_salesman.open sql,conn,1,1

				while not rs_salesman.eof
			%>
				<option value="<%=rs_salesman("salesman_id")%>" <%if rs_salesman("salesman_id")=rs("customer_salesman") then response.write("selected") %>><%=rs_salesman("salesman_name")%></option>
			<%
					rs_salesman.movenext()
				Wend
				rs_salesman.close
				set rs_salesman=nothing
			%>

			</select>
		</td>
	</tr>
	<tr>
		<th>客户管理人</th>
		<td><%
					if rs("customer_user")=session("MM_ID") or isnull(rs("customer_user")) or CheckPF(1,15) then
				%>
				<select id="customer_user" name="customer_user">
					<option value="0"></option>
				<%
					sql="select * from yh "
					set rs_yh=server.createobject("adodb.recordset")
					rs_yh.open sql,conn,1,1
					while not rs_yh.eof
				%>
					<option value="<%=rs_yh("id")%>" <%if rs_yh("id")=rs("customer_user") then response.write("selected") %>><%=rs_yh("name")%></option>
				<%
						rs_yh.movenext()
					wend
					rs_yh.close
					set rs_yh=nothing
				%>
				</select>
				<%
					else
				%>
				<%=rs("name")%>				
				<input name="customer_user" type="hidden" value="<%=rs("id")%>" />
				<%
					end if
				%>
		</td>
	</tr>
	<tr>
		<th colspan="2"><input class="bt" type="submit" name="tj" id="tj" value="编辑" /></th>
	</tr>
</table>
</form>
<div id="map_canvas" style="width: 100%; height: 500px"></div>
<%
	rs.close
	set rs=nothing
	conn.close
	set conn=nothing
%>
</body>
</html>
